<?php

namespace app\site\controller\site;

use app\common\exception\MiException;
use app\model\site\UserLevel;
use app\Request;
use app\site\controller\AController;
use app\site\controller\traits\CurdTrait;
use app\site\service\site\UserLevelService;
use app\site\service\SiteReqService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class UserLevelController extends AController
{

    /**
     * @throws DbException
     */
    public function index(){
        return $this->reqSer->setService(UserLevelService::instance())->setPaginate()->getResponse();
    }

    /**
     * @throws MiException
     */
    public function add(){
        return $this->reqSer->setService(UserLevelService::instance())->add()->getResponse();
    }

    /**
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws MiException
     */
    public function edit(Request $request){
        if($request->isPost()){
            return $this->reqSer->setService(UserLevelService::instance())->edit()->getResponse();
        }else{
            return $this->reqSer->setService(UserLevelService::instance())->setFind()->getResponse();
        }
    }

    /**
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws MiException
     */
    public function del(){
        return $this->reqSer->setService(UserLevelService::instance())->delete()->getResponse();
    }

    /**
     * 会员等级选择器
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function options(): \think\response\Json
    {
        $list = UserLevelService::instance()->setField('id,title')->setWhere(['is_show'=>1])->select();
        return jsonSuccess([
            'options' => $list,
        ]);
    }

}
